﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NegevGas.Core
{
    public class ConfigProperty
    {
        // Fields
        internal ConfigPropertyDefinition _definition;
        internal int _id;
        private object _value;

        // Methods
        internal ConfigProperty(ConfigPropertyDefinition propDef, int id)
            : this(propDef, id, null)
        {
        }

        internal ConfigProperty(ConfigPropertyDefinition propDef, int id, string value)
        {
            this._id = id;
            this._definition = propDef;
            this.SetValue(value);
        }

        public void SetValue(string value)
        {
            if (value != null)
            {
                switch (this.Type)
                {
                    case ConfigPropertyType.Text:
                        this._value = value;
                        return;

                    case ConfigPropertyType.Number:
                        this._value =Convert.ToInt32(value);
                        return;

                    case ConfigPropertyType.Boolean:
                        this._value = (value.ToLower() == "true") ? ((object)1) : ((object)(value.ToLower() == "1"));
                        return;
                }
            }
            else
            {
                switch (this.Type)
                {
                    case ConfigPropertyType.Text:
                        this._value = string.Empty;
                        return;

                    case ConfigPropertyType.Number:
                        this._value = 0;
                        return;

                    case ConfigPropertyType.Boolean:
                        this._value = false;
                        return;
                }
            }
        }

        // Properties
        public string Alias
        {
            get
            {
                return this._definition.Alias;
            }
        }

        public object DefaultValue
        {
            get
            {
                return this._definition.DefaultValue;
            }
        }

        public string Group
        {
            get
            {
                return this._definition.Group;
            }
        }

        public int Id
        {
            get
            {
                return this._id;
            }
        }

        public string ModuleName
        {
            get
            {
                return this._definition.ModuleName;
            }
        }

        public string Name
        {
            get
            {
                return this._definition.Name;
            }
        }

        public ConfigPropertyType Type
        {
            get
            {
                return this._definition.Type;
            }
        }

        public object Value
        {
            get
            {
                return this._value;
            }
            set
            {
                this._value = value;
            }
        }
    }


}